local fs  = require "nixio.fs"
local sys = require "luci.sys"
local utl = require "luci.util"
local ds =  require "luci.dispatcher"
local http = require "luci.http"
local pt = require "luci.http.protocol"
local nt = require "luci.model.nac_nsutil"


m = SimpleForm("user_cfg", translate("NAC user management"),
	translate("NAC local user management"))
m.reset = false
m.submit = false


-- 进行查询，并将查询结果传入到view
local ubusrt = utl.ubus("nac_proxy", "websrv", {act='getalluser',data={ opt=1 }}) or {msg="ubus error",rt_code="0"}


local rt_code = ubusrt["rt_code"] or ""
if rt_code ~= 200 then
    nt.error_render(m, rt_code, ubusrt["msg"])
    return m
end

local msg = ubusrt["msg"] or ""


if type(msg) ~= "table" then
    --不是table，表示服务器存在问题
    nt.error_render(m, rt_code, translate("Server Error"))
    return m
elseif msg.code == 1 then
    -- 服务返回的code为1，表示该次请求存在错误
    nt.error_render(m, rt_code, translate("Request error"))
    return m
elseif msg.code == 2 then
    -- 服务返回的code为2，表示该次查询结果为空
else
    -- 正确，返回code=200,data为服务返回的内容
end


local user_list = {}
if msg.obja ~= nil then
  for k,v in pairs(msg.obja) do
      table.insert(user_list, v)
  end
end
s = m:section(SimpleSection)
s.template = "cbi/blanksection"

-- -- alert控件
op = s:option(Button, "deluser", translate("delete"))
msg_code = http.formvalue("msg_code")
error_msg = ""
if msg_code == "-1" then
    error_msg = "delete failed"
elseif msg_code == "3" then
    error_msg = "system error"
end
if #error_msg > 0 then 
    op.rt_code = msg_code
else
    op.rt_code = http.formvalue("code") or 200
end
op.error_msg = translate(error_msg)
op.template  = "nac/alert"

if msg.usernum == nil then
	msg.usernum = "0"
end



t = m:section(Table, user_list, translate(translate("Number of local users : ").. msg.usernum))
t.anonymous = true
t.addremove = true
t.userdef = true
t.extadd = luci.dispatcher.build_url("admin/ac_nac/localuser","new")
t.template = "nac/tblsection"
t.paging = true
if #user_list > 0 then
    t.paging = true
else
    t.paging = false
end

op = t:option(DummyValue, "id", translate("ID"))
function op.cfgvalue(self, section)
    return user_list[section].id
end

t:option(DummyValue, "user_name", translate("User name"))



op = t:option(DummyValue, "session_time", translate("Session time"))
function op.cfgvalue(self, section)
  -- return user_list[section].session_time
   return nt.format_life(user_list[section].session_time) 
end

op = t:option(DummyValue, "flow", translate("Flow quota"))
function op.cfgvalue(self, section)
   return nt.format_traffic(user_list[section].flow)
end


op = t:option(DummyValue, "rate_down", translate("Download speed"))
function op.cfgvalue(self, section)
   return nt.format_rate(user_list[section].rate_down)
end

op = t:option(DummyValue, "rate_up", translate("Upload rate"))
function op.cfgvalue(self, section)
    return nt.format_rate(user_list[section].rate_up)
end

op = t:option(DummyValue, "state", translate("state"))
function op.cfgvalue(self, section)
   if user_list[section].state == 1 then
    return  translate("Enabled")
   else
    return translate("disabled")
   end
end



-- AP详细信息
e = t:option(Button, "detail", translate("detailed"))
e.inputstyle = "edit"
e.template = "nac/btn_detail"
--cbi-button cbi-input-remove
e.write = function(self, section)
    http.redirect(ds.build_url("admin/ac_nac/localuser", "detail").."?user_name="..user_list[section].user_name)
end


-- AP下用户列表
ex = t:option(Button, "editone", translate("Edit"))
ex.inputstyle = "edit"
-- 自定义按钮
ex.render = function(self, section, scope)
    ex.user_name = user_list[section].user_name or ""
    ex.template  = "nac/btn_edit"
    -- 下一个URL
    ex.nurl = ds.build_url("admin/ac_nac/localuser/edit").. "?user_name=" .. ex.user_name
    Button.render(self, section, scope) 
end


-- 重启AP
e = t:option(Button, "remove", translate("delete"))
e.inputstyle = "remove"
-- 自定义按钮
e.render = function(self, section, scope)
    e.template  = "nac/btn_delete"
    e.user_name = user_list[section].user_name
 --   e.info = "delete"
    -- 下一个URL
    e.nurl = ds.build_url("admin/ac_nac/localuser_delete", e.user_name)
    e.confirm = translate("Are you sure you want to delete the user?")
    Button.render(self, section, scope)
end

return m
